<?php

// +----------------------------------------------------------------------
// | author: admin
// +----------------------------------------------------------------------

namespace app\adminapi\logic\travel;


use app\common\model\travel\LinePrice;
use app\common\logic\BaseLogic;
use app\common\model\travel\LinePriceStatusLog;
use think\facade\Db;


/**
 * LinePrice逻辑
 * Class LinePriceLogic
 * @package app\adminapi\logic\travel
 */
class LinePriceLogic extends BaseLogic
{


    /**
     * @notes 添加
     * @param array $params
     * @return bool
     * @author admin
     * @date 2025/04/29 09:56
     */
    public static function add(array $params): bool
    {
        Db::startTrans();
        try {
            LinePrice::create([
                'line_id' => $params['line_id'],
                'price' => $params['price'],
                'travel_time' => strtotime($params['travel_time']),
                'user_id' => $params['user_id'],
            ]);

            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            self::setError($e->getMessage());
            return false;
        }
    }


    /**
     * @notes 编辑
     * @param array $params
     * @return bool
     * @author admin
     * @date 2025/04/29 09:56
     */
    public static function edit(array $params): bool
    {
        Db::startTrans();
        try {
            LinePrice::where('id', $params['id'])->update([
                'line_id' => $params['line_id'],
                'price' => $params['price'],
                'travel_time' => strtotime($params['travel_time']),
                'user_id' => $params['user_id'],
            ]);

            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            self::setError($e->getMessage());
            return false;
        }
    }


    /**
     * @notes 删除
     * @param array $params
     * @return bool
     * @author admin
     * @date 2025/04/29 09:56
     */
    public static function delete(array $params): bool
    {
        return LinePrice::destroy($params['id']);
    }


    /**
     * @notes 获取详情
     * @param $params
     * @return array
     * @author admin
     * @date 2025/04/29 09:56
     */
    public static function detail($params): array
    {
        return LinePrice::findOrEmpty($params['id'])->toArray();
    }

    public static function updateStatus(array $params)
    {
        LinePrice::update([
            'id' => $params['id'],
            'status' => $params['status']
        ]);

        LinePriceStatusLog::create([
            'line_price_id' => $params['id'],
            'user_id' => $params['user_id'],
            'status' => $params['status'],
            'remark' => $params['comment']
        ]);
        return true;
    }
}